package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.Date.DateUtil;
import org.jeecg.modules.appuser.constant.LuckDrawConstant;
import org.jeecg.modules.appuser.constant.PayTypeConstant;
import org.jeecg.modules.appuser.entity.TbUserIntegral;
import org.jeecg.modules.appuser.entity.TbUserIntegralDetail;
import org.jeecg.modules.appuser.entity.TbUserLuckdraw;
import org.jeecg.modules.appuser.entity.TbUserLuckdrawDetail;
import org.jeecg.modules.appuser.mapper.TbUserLuckdrawDetailMapper;
import org.jeecg.modules.appuser.service.ITbUserIntegralDetailService;
import org.jeecg.modules.appuser.service.ITbUserIntegralService;
import org.jeecg.modules.appuser.service.ITbUserLuckdrawDetailService;
import org.jeecg.modules.appuser.service.ITbUserLuckdrawService;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 抽奖明细表
 * @Author: jeecg-boot
 * @Date: 2020-05-18
 * @Version: V1.0
 */
@Service
public class TbUserLuckdrawDetailServiceImpl extends ServiceImpl<TbUserLuckdrawDetailMapper, TbUserLuckdrawDetail> implements ITbUserLuckdrawDetailService {

    @Autowired
    @Lazy(true)
    private ITbUserLuckdrawService tbUserLuckdrawService;

    @Autowired
    private ITbUserIntegralService tbUserIntegralService;

    @Autowired
    private ITbUserIntegralDetailService tbUserIntegralDetailService;

    @Autowired
    @Lazy(true)
    private ITbMnUserService tbMnUserService;

    @Override
    public Result<?> joinLuckDraw(TbUserLuckdrawDetail tbUserLuckdrawDetail) {
        //结束前10分钟不能参加,判断SQT是否充足，判断抽奖是否结束,扣除SQT
        if (null == tbUserLuckdrawDetail) {
            return Result.error("参数错误");
        }
        if (null == tbUserLuckdrawDetail.getLuckdrawId()  || null == tbUserLuckdrawDetail.getUserId()) {
            return Result.error("参数错误");
        }
        //查询用户积分
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", tbUserLuckdrawDetail.getUserId());
        TbUserIntegral tbUserIntegral = tbUserIntegralService.getOne(queryWrapper);

        if (null == tbUserIntegral) {
            return Result.error("积分不存在");
        }
        TbUserLuckdraw tbUserLuckdraw = tbUserLuckdrawService.getById(tbUserLuckdrawDetail.getLuckdrawId());
        if(null==tbUserLuckdraw){
            return Result.error("抽奖不存在");
        }
        Date entTime = tbUserLuckdraw.getEndTime();
        if (DateUtil.bjDate(new Date(), entTime)) {
            return Result.error("不能参加抽奖");
        }

        if (tbUserLuckdraw.getState() != 1) {
            return Result.error("抽奖已结束");
        }
        TbMnUser tbMnUser = tbMnUserService.getById(tbUserLuckdrawDetail.getUserId());
        //0为第一次抽奖
        //去掉第一次免积分抽奖，改成-1
        if (tbMnUser.getFirstLuckdrawStatus() == -1) {
            save(tbUserLuckdrawDetail);
            tbMnUser.setFirstLuckdrawStatus(1);
            tbMnUserService.updateById(tbMnUser);
        } else {
            BigDecimal joinIntegral = tbUserLuckdraw.getJoinIntegral();
            BigDecimal myIntegral = tbUserIntegral.getIntegral();

            int flag = myIntegral.compareTo(joinIntegral);
            if (flag == -1) {
                return Result.error("积分不足，努力做任务吧");
            }
            BigDecimal newbigdecimal = myIntegral.subtract(joinIntegral);
            tbUserIntegral.setIntegral(newbigdecimal);
            if (tbUserIntegralService.updateById(tbUserIntegral)) {
                if (saveIntegralDetail(tbUserIntegral, joinIntegral, newbigdecimal)) {
                    save(tbUserLuckdrawDetail);
                }
            }
        }

        return Result.ok("参与成功！");

    }

    private boolean saveIntegralDetail(TbUserIntegral tbUserIntegral, BigDecimal joinIntegral, BigDecimal newbigdecimal) {
        TbUserIntegralDetail tbUserIntegralDetail = new TbUserIntegralDetail();
        tbUserIntegralDetail.setBalance(joinIntegral);
        tbUserIntegralDetail.setFromType(PayTypeConstant.PAYFORM_LUCKDRAW);
        tbUserIntegralDetail.setInOrOut(PayTypeConstant.PAYTYPE_EXPEND);
        tbUserIntegralDetail.setIntegralId(tbUserIntegral.getId());
        tbUserIntegralDetail.setBalanceAfter(newbigdecimal);
        tbUserIntegralDetail.setUserId(tbUserIntegral.getUserId());
        tbUserIntegralDetail.setState(1);
        return tbUserIntegralDetailService.save(tbUserIntegralDetail);
    }


}
